package com.carAnalysisAdmin.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.carAnalysisAdmin.system.domain.UserSpotInfo;
import com.carAnalysisAdmin.system.mapper.UserSpotInfoMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.carAnalysisAdmin.common.annotation.Log;
import com.carAnalysisAdmin.common.core.controller.BaseController;
import com.carAnalysisAdmin.common.core.domain.AjaxResult;
import com.carAnalysisAdmin.common.enums.BusinessType;
import com.carAnalysisAdmin.system.domain.SysParkSpot;
import com.carAnalysisAdmin.system.service.ISysParkSpotService;
import com.carAnalysisAdmin.common.utils.poi.ExcelUtil;
import com.carAnalysisAdmin.common.core.page.TableDataInfo;

/**
 * 车位Controller
 * 
 * @author ruoyi
 * @date 2024-04-14
 */
@RestController
@RequestMapping("/system/spot")
public class SysParkSpotController extends BaseController
{
    @Autowired
    private ISysParkSpotService sysParkSpotService;
    @Autowired
    private UserSpotInfoMapper userSpotInfoMapper;

    /**
     * 查询车位列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysParkSpot sysParkSpot)
    {
        startPage();
        List<SysParkSpot> list = sysParkSpotService.selectSysParkSpotList(sysParkSpot);
        list.forEach(item ->{
            UserSpotInfo userSpotInfo = userSpotInfoMapper.selectUserSpotInfoByCode(item.getCode());
            if (userSpotInfo != null){
                item.setUserName(userSpotInfo.getUserName());
                item.setUserAvatar(userSpotInfo.getUserAvatar());
                item.setUserId(userSpotInfo.getUserId());
                item.setIsHaveUser("1");
            }else {
                item.setIsHaveUser("0");
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出车位列表
     */
    @Log(title = "车位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysParkSpot sysParkSpot)
    {
        List<SysParkSpot> list = sysParkSpotService.selectSysParkSpotList(sysParkSpot);
        ExcelUtil<SysParkSpot> util = new ExcelUtil<SysParkSpot>(SysParkSpot.class);
        util.exportExcel(response, list, "车位数据");
    }

    /**
     * 获取车位详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysParkSpotService.selectSysParkSpotById(id));
    }

    /**
     * 新增车位
     */
    @Log(title = "车位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysParkSpot sysParkSpot)
    {
        return toAjax(sysParkSpotService.insertSysParkSpot(sysParkSpot));
    }

    /**
     * 修改车位
     */
    @Log(title = "车位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysParkSpot sysParkSpot)
    {
        return toAjax(sysParkSpotService.updateSysParkSpot(sysParkSpot));
    }

    /**
     * 删除车位
     */
    @Log(title = "车位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysParkSpotService.deleteSysParkSpotByIds(ids));
    }

    @Log(title = "车位可用", businessType = BusinessType.UPDATE)
    @PutMapping("/handleUsable/{ids}")
    public AjaxResult handleUsable(@PathVariable Long[] ids){
        return toAjax(sysParkSpotService.handleUsable(ids));
    }

    @Log(title = "车位禁用", businessType = BusinessType.UPDATE)
    @PutMapping("/handleDisabled/{ids}")
    public AjaxResult handleDisabled(@PathVariable Long[] ids){
        return toAjax(sysParkSpotService.handleDisabled(ids));
    }

}
